<template>
	<view>
				<view class="uni-list"  style="overflow: auto;height: 330px;border: 1px solid #55aaff;">
					<checkbox-group @change="checkboxChange">
						<label class="uni-list-cell uni-list-cell-pd" v-for="item in items" :key="item.value">
							<view>
								<checkbox :value="item.value" :checked="item.checked" />
							</view>
							<view>{{item.name}}</view>
						</label>
					</checkbox-group>
				</view>
				
				<view class="fw"  @click="addcd()">添加</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				uid:'',
				items: []
			}
		},
		async onLoad(){
			this.getclistall();
		},
		methods: {
			async addcd(){
					var that = this;
					let cdidall = []
					that.items.forEach(item => {
						if (item.checked == true) {
							// console.log(item)←————重点 获取选中的值放到数组中
							cdidall.push(item.id);  
						}
					});

				let data = {
					uid:uni.getStorageSync('uid'),
					cdidall:cdidall
				}
				//获取全部选中项
				
				let res =  await this.$api.addhzcycdlist(data);
				console.log(res.data)
				if(res.data.code==0){
						uni.navigateTo({
							url:"/pages/index/index"
						})
				}
			},
			async getclistall(){
				let res =  await this.$api.getclistall()
				if(res.data.code==0){
					this.items = res.data.data
				}
			},
			checkboxChange: function (e) {
							var items = this.items,
								values = e.detail.value;
							for (var i = 0, lenI = items.length; i < lenI; ++i) {
								const item = items[i]
								if(values.includes(item.value)){
									this.$set(item,'checked',true)
								}else{
									this.$set(item,'checked',false)
								}
							}
						}
		}
	}
</script>

<style>
.uni-list-cell {
	justify-content: flex-start
}
	.fw{
		width: 600upx;
		height: 100upx;
		border: 1upx solid #007AFF;
		text-align: center;
		line-height: 100upx;
		margin: 0 auto;
		margin-top: 30upx;
	}
</style>
